from apps.models.t_dev_data_model import TDevData


class TDevDataSerializer:
    """
    TDevData 序列化器类
    """

    @staticmethod
    def serialize(dev_data):
        """
        将DevData实例序列化为字典
        """
        if not dev_data:
            return None

        return {
            'id': dev_data.id,
            'data_type': dev_data.data_type,
            'data_id': dev_data.data_id,
            'dev_id': dev_data.dev_id,
            'reg_id': dev_data.reg_id,
            'data_value': dev_data.data_value,
            'data_time': dev_data.data_time
        }

    @staticmethod
    def serialize_list(dev_data_list):
        """
        将DevData实例列表序列化为字典列表
        """
        return [TDevDataSerializer.serialize(dev_data) for dev_data in dev_data_list]

    @staticmethod
    def deserialize(data):
        """
        将字典反序列化为DevData实例
        """
        if not data:
            return None

        dev_data = TDevData()
        dev_data.id = data.get('id', None)
        dev_data.data_type = data.get('data_type', '')
        dev_data.data_id = data.get('data_id', 0)
        dev_data.dev_id = data.get('dev_id', 0)
        dev_data.reg_id = data.get('reg_id', 0)
        dev_data.data_value = data.get('data_value', 0.0)
        dev_data.data_time = data.get('data_time', '')

        return dev_data